package leetcode.editor.cn;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Dempsey
 * @Data: 2021-03-12 14:52:18
 */

//给定一个排序的整数数组 nums ，其中元素的范围在 闭区间 [lower, upper] 当中，返回不包含在数组中的缺失区间。 
//
// 示例： 
//
// 输入: nums = [0, 1, 3, 50, 75], lower = 0 和 upper = 99,
//输出: ["2", "4->49", "51->74", "76->99"]
// 
// Related Topics 数组 
// 👍 37 👎 0


public class P163 {
    public static void main(String[] args) {
        Solution solution = new P163().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public List<String> findMissingRanges(int[] nums, int lower, int upper) {
            List<String> result = new ArrayList<>();
            int last = lower;

            for (int i = 0; i <= nums.length; i++) {
                int curr;
                if (i == nums.length) curr = upper + 1;
                else curr = nums[i];
                if (last == curr) {
                    last += 1;
                    continue;
                }
                if (last - curr == -1) {
                    result.add(String.valueOf(last));
                    last = curr + 1;
                    continue;
                }
                if (last - curr < -1) {
                    result.add(last + "->" + (curr - 1));
                    last = curr + 1;
                    continue;
                }
            }

            return result;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}